package cc.comac.mom.service.base;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import cc.comac.mom.util.ExcelColumn;
import cc.comac.mom.util.ExportUtil;
import cc.comac.mom.util.ImportUtil;
import cc.comac.mom.util.ReflectionUtil;
import lombok.Data;
import lombok.NonNull;

public abstract class BaseService {


    @Autowired
    protected ExportUtil exportUtil;
    @Autowired
    protected ImportUtil importUtil;

  

    protected void copyPropertyWithoutLogInfo(@NonNull Object src, @NonNull Object target) {
        BeanUtils.copyProperties(src, target, "id", "version", "creator", "creationDate", "lsdModifier", "modifiedDate");
    }

  

    protected <T> XSSFWorkbook download(String sheetName, Class<T> clz) {
        List<Field> fields = ReflectionUtil.getFieldAnnotatedWith(clz, ExcelColumn.class, "template");
        List<String> columnNames = fields.stream().map(field -> field.getAnnotation(ExcelColumn.class).text()).collect(Collectors.toList());
        List<String> requiredList = columnNames;
        Map<Integer, List<String>> optionsMap = new HashMap<>();
        XSSFWorkbook workbook = new XSSFWorkbook();
        return exportUtil.executeSheet(workbook, sheetName, "", columnNames, requiredList, new ArrayList<>(), new ArrayList<>(), optionsMap, false);
    }

    @Data
    public static class DictionaryDTO {
        private String code;
        private String display;
    }
}
